package cn.tedu.flower.manage.content.dao.persist.repository.impl;

import cn.tedu.flower.manage.common.pojo.vo.PageData;
import cn.tedu.flower.manage.common.util.PageInfoToPageDataConverter;
import cn.tedu.flower.manage.content.dao.persist.mapper.AddressMapper;
import cn.tedu.flower.manage.content.dao.persist.repository.IAddressRepository;
import cn.tedu.flower.manage.content.pojo.entity.Address;
import cn.tedu.flower.manage.content.pojo.vo.AddressListItemVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
@Slf4j
@Repository
public class AddressRepositoryImpl implements IAddressRepository{

    @Autowired
    AddressMapper addressMapper;

    @Override
    public int insert(Address address){
        log.debug("开始执行【插入地址】的数据访问，参数：{}", address);
        return addressMapper.insert(address);
    }

    @Override
    public int deleteById(Long id){
        log.debug("开始执行【删除地址】的数据访问，参数：{}",id);
        return addressMapper.deleteById(id);
    }

    @Override
    public int updateDefaultById(Long id, Integer isDefault) {
        log.debug("开始执行【根据id修改默认地址】，id：{}，新的启用状态：{}", id, isDefault);
        Address address = new Address();
        address.setIsDefault(isDefault);
        /*QueryWrapper是由Mybatis plus框架提供的一个查询器*/
        QueryWrapper<Address> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        return addressMapper.update(address, wrapper);
    }

    @Override
    public int updateById(Address address){
        log.debug("开始执行【更新地址】的数据访问，参数：{}",address);
        return addressMapper.updateById(address);
    }

    @Override
    public AddressListItemVO getAddressByUsername(String username){
        AddressListItemVO list= addressMapper.getAddressByUsername(username);
        return list;
    }

    @Override
    public PageData<AddressListItemVO> getAddressAll (Integer pageNum, Integer pageSize) {
        log.debug("开始执行【查询标签列表】，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<AddressListItemVO> list = addressMapper.getAddressAll();
        PageInfo<AddressListItemVO> pageInfo = new PageInfo<>(list);
        PageData<AddressListItemVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }
}
